package com.fjnu.controller.allocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.InstrumentController;
import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.MainInstrument;
import com.fjnu.entities.ReserveLand;
import com.fjnu.entities.Unit;
import com.fjnu.service.IReserveLandService;
import com.fjnu.service.IUnitService;
import com.fjnu.service.InstrumentService;
import com.fjnu.service.impl.ReserveLandService;

/**
 * 完成资产的调拨
 * @author 默默
 *
 */
@Controller
public class allocationBorrowController extends BaseController{
	private static Logger logger =LogManager.getLogger(allocationBorrowController.class);
	@Autowired
	private InstrumentService instrumentService;
	@Autowired
	private IReserveLandService reserveLandService;
	@Autowired
	private IUnitService unitService;
	/**
	 * 对单个资产进行调拨到哪个地方
	 * @param mainId 设备id
	 * @param landId 目标存放地id
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping("/oneAllocationLand")
	@ResponseBody
	public Map<String,Object> oneAllocationLand(Integer mainId,Integer landId){
		logger.info("单个资产的调拨到存放地");
		if(!init(mainId,landId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		MainInstrument mainInstrument = instrumentService.findUniqueInstrument(mainId);
		ReserveLand reserveland =reserveLandService.findUniqueRes(landId);
		mainInstrument.setReserveland(reserveland);
		instrumentService.updateInstrument(mainInstrument);
		return getData2Result();
	}
	/**
	 * 单个资产调拨到某个单位
	 * @param mainId 设备Id
	 * @param unitId 单位Id
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping("/oneAllocationUnit")
	@ResponseBody
	public Map<String,Object> oneAllocationUnit(Integer mainId,Integer unitId){
		logger.info("单个资产的调拨单位");
		if(!init(mainId,unitId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		MainInstrument mainInstrument = instrumentService.findUniqueInstrument(mainId);
		//ReserveLand reserveland =reserveLandService.findUniqueRes(landId);
		//mainInstrument.setReserveland(reserveland);
		Unit unit =unitService.findUniqueUnit(unitId);
		mainInstrument.setUnit(unit);
		instrumentService.updateInstrument(mainInstrument);
		return getData2Result();
	}
	/**
	 * 批量资产调拨到存放地
	 * @param ids  设备id列表字符串
	 * @param landId 存放地id
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping("/manyAllocationLand")
	@ResponseBody
	public Map<String,Object> manyAllocationLand(String ids,Integer landId){
		logger.info("批量资产调拨到存放地");
		if(!init(ids,landId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		String[] arr = new String[ids.length()];
		ReserveLand reserveland =reserveLandService.findUniqueRes(landId);
		for(int i=0;i<ids.length();i++){
			arr[i]=ids.substring(i, i+1);
			MainInstrument mainInstrument = instrumentService.findUniqueInstrument(Integer.parseInt(arr[i]));
			mainInstrument.setReserveland(reserveland);
			instrumentService.updateInstrument(mainInstrument);
		}
		return getData2Result();
	}
	
	/**
	 * 批量设备调拨到某个单位
	 * @param ids 设备id列表
	 * @param unitId 单位id
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping("/manyAllocationUnit")
	@ResponseBody
	public Map<String,Object> manyAllocationUnit(String ids,Integer unitId){
		logger.info("批量设备调拨到某个单位");
		if(!init(ids,unitId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		String[] arr = new String[ids.length()];
		Unit unit = unitService.findUniqueUnit(unitId);
		for(int i=0;i<ids.length();i++){
			arr[i]=ids.substring(i, i+1);
			MainInstrument mainInstrument = instrumentService.findUniqueInstrument(Integer.parseInt(arr[i]));
			//ReserveLand reserveland =reserveLandService.findUniqueRes(landId);
			mainInstrument.setUnit(unit);
			instrumentService.updateInstrument(mainInstrument);
		}
		return getData2Result();
	}
}
